#pragma once

/*
 * anet tcp loop.
 */

#include "thread_pool.hpp"
#include "define.hpp"
#include "asio/detail/noncopyable.hpp"

namespace anet {
	namespace tcp {
		class CEventLoop final: asio::noncopyable {
		public:
			explicit CEventLoop(unsigned int size) : m_threadPool(size) {
				m_threadPool.Init();
			}
			virtual ~CEventLoop() {
				m_threadPool.Stop();
			}

		public:
			// connection context.
			auto& getNextIOContext() {
				return m_threadPool.GetIoContext();
			}

			// acceptor context.
			auto& getAcceptorIOContext() {
				return m_threadPool.GetAcceptorIoContext();
			}
			
			// run
			void run() {
				m_service.run();
			}
			
			// wait to terminate
			void wait() {
				m_threadPool.Join();
			}

			// post to do in m_service
			void post(std::function<void ()> func) {
				asio::post(m_service, std::move(func));
			}

		private:
			// thread pool
			CThreadPool m_threadPool;

			// asio context, for future main thread.
			asioIOContext m_service;
		};
	}
}